﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.ViewModels;
using Manager.Common;
using Manager.App_Code;
using System.Linq;

namespace Manager.FormAdd_Edit
{
    public partial class frm_ThemDichVuKhac : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        public DichVuViewModel vmDichVu = null;

        public frm_ThemDichVuKhac()
        {
            InitializeComponent();
        }        

        private void frm_ThemDichVuKhac_Load(object sender, EventArgs e)
        {
            cmbTienTe.DataSource = db.TienTes.ToList();
            cmbTienTe.DisplayMember = "TenDonVi";
            cmbTienTe.ValueMember = "MaDonVi";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtGiaTien.Text == "" || txtTenDichVu.Text == "" || txtSoLuong.Text == "")
            {
                MessageBox.Show("Vui lòng điền đầy đủ thông tin.");
                return;
            }
            vmDichVu = new DichVuViewModel();
            vmDichVu.LoaiDichVu = LoaiDichVu.PhatSinhThem;
            var nhomDV = db.NhomDichVus.SingleOrDefault(t => t.MaNhomDV == 4);//get nhom dich vu khac
            vmDichVu.NhomDichVu = nhomDV.MaNhomDV;
            vmDichVu.TenNhomDichVu = nhomDV.TenNhomDV;
            vmDichVu.TenDichVu = txtTenDichVu.Text;
            vmDichVu.SoLuong = CommonFunction.ParseInt(txtSoLuong.Text);
            vmDichVu.GiaDV = CommonFunction.ParseDouble(txtGiaTien.Text);
            var tienTe = cmbTienTe.SelectedItem as TienTe;
            vmDichVu.DonViTienTe = tienTe.MaDonVi;
            vmDichVu.TenDonViTienTe = tienTe.TenDonVi;
            vmDichVu.NgayTao = DateTime.Now;
            vmDichVu.ThanhTien = vmDichVu.SoLuong * vmDichVu.GiaDV;
            vmDichVu.ThanhTienVND = vmDichVu.ThanhTien.Value * tienTe.TyGiaThamKhao.Value;
            
            this.Close();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtGiaTien_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}